﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace CashFlow.Common
{
	public class DateTimeUtil
	{
		public DateTime ParseYYMM(string yearMonth)
		{
			Contract.Requires(!string.IsNullOrEmpty(yearMonth), "yearMonth value should has a value");
			//TODO: vvs p1 add contract to test regex value format YYMM

			var year = 2000 + int.Parse(yearMonth.Substring(0, 2));
			var month = int.Parse(yearMonth.Substring(2, 2));

			return new DateTime(year, month, 1);
		}

		public string ToYYMM(DateTime date)
		{
			//TODO: vvs p3 add contract return value format should be YYMM
			return date.ToString("yyMM");
		}

		public DateTime NextMonth(DateTime date)
		{
			return date.AddMonths(1); 
		}

		public DateTime PrevMonth(DateTime date)
		{
			return date.AddMonths(-1);
		}

		public DateTime MonthFirstDate(DateTime value)
		{
			return new DateTime(value.Year, value.Month, 1);
		}

		public DateTime MonthLastDate(DateTime value)
		{
			var lastDay = MonthFirstDate(value).AddMonths(1).AddDays(-1);
			return lastDay; ;
		}

		public IEnumerable<DateTime> GetMonths(DateTime from, DateTime to)
		{
			var months = new List<DateTime>();
			var nextDate = MonthFirstDate(from);
			while (nextDate <= to)
			{
				months.Add(nextDate);
				nextDate = nextDate.AddMonths(1);
			}

			return months;
		}

	}
}
